import 'dart:io';

import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sharesdk_plugin/sharesdk_defines.dart';
import 'package:sharesdk_plugin/sharesdk_interface.dart';
import 'package:sharesdk_plugin/sharesdk_map.dart';
import 'package:sph_driver/AppConfig/Config.dart';
import 'package:sph_driver/AppConfig/LocalStorage.dart';
import 'package:sph_driver/Login/LoginVIewController.dart';
import 'package:sph_driver/Main/Settings/AccountAndSecurity.dart';
import 'package:sph_driver/Main/Settings/MessageSetting.dart';
import 'package:sph_driver/Main/Settings/NetworkDetection.dart';
import 'package:sph_driver/Main/Settings/PrivacyPolicyList.dart';
import 'package:sph_driver/Main/Settings/PrivacyPolicyManagement.dart';
import 'package:sph_driver/Main/Settings/UserAddress.dart';
import 'package:sph_driver/Main/Settings/VoiceSetting.dart';
import 'package:sph_driver/http/NoLoadingGetApi.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../AppConfig/RouteAct.dart';
import '../../Login/LoginVIewController.dart';
import '../../l10n/messages.dart';
import '../../main.dart';
import 'AboutSPH.dart';
import 'CacheUtil.dart';

class SettingList extends StatefulWidget {
  const SettingList({super.key});

  @override
  State<SettingList> createState() => _SettingListState();
}

class _SettingListState extends State<SettingList> {
  @override
  final TextStyle titleStyle=TextStyle(fontWeight: FontWeight.w100);



  String appCache="0M";
  void _toggleLanguage(BuildContext context) {
    var currentLocale = Localizations.localeOf(context);
    var newLocale = currentLocale.languageCode == 'en'
        ? const Locale('zh', '')
        : const Locale('en', '');

    MyApp.setLocale(context, newLocale);
  }
  @override
  void initState(){
    super.initState();
    getAppCache();
  }

// 获取缓存的大小
  void getAppCache() {
    CacheUtil.loadApplicationCache().then((value) {
      // 获取到缓存大小
      appCache = CacheUtil.formatSize(value);
    });
  }
  void _copyToClipboard(String copyNumber) {
    Clipboard.setData(ClipboardData(text: copyNumber));
    BrnToast.show('复制成功', context);

  }

  Widget build(BuildContext context) {

    var messages = AppLocalizations.of(context)!;
    return Scaffold(
      appBar: AppBar(
        title: Text(messages["设置"],style: TextStyle(fontWeight: FontWeight.w300,fontSize: 16.sp),),
        centerTitle: true,
        backgroundColor: Colors.white,

      ),
      backgroundColor: Color.fromRGBO(245, 245, 245, 1),
      body:Column(
        children: [
          Expanded(child:

          ListView(

            children: [
              SizedBox(height: 8.h,),
              GestureDetector(
                child:   Container(
                  padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 8.h,),
                  color: Colors.white,
                  height: 35.h,
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(messages["语言设置"],style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                          Row(
                            children: [
                              // Text("简体中文",style: TextStyle(color: MyColors.authTipColor,fontSize: 14.sp),),
                              Icon(Icons.arrow_forward_ios,color: MyColors.authTipColor,size: 14,),
                            ],
                          )
                        ],
                      ),
                      SizedBox(height: 8.h,),
                      Divider(height: 1,color: MyColors.backgreyColor,),
                    ],
                  ),
                ),
                onTap: () {
                  showDialog(
                    barrierDismissible: false,
                    context: context,
                    builder: (BuildContext context) {
                      return BrnDialog(
                        titleText: messages["是否切换语言"],
                        actionsWidget: [
                          TextButton(
                            onPressed: () {
                              Navigator.pop(context);
                            },
                            child: Text( messages['取消'],style: TextStyle(color: MyColors.textColor,fontSize: 14.sp),),
                          ),
                          TextButton(
                            onPressed: () {
                              Navigator.pop(context);
                              _toggleLanguage(context);
                            },
                            child: Text(messages['确定'],style: TextStyle(color: MyColors.themeColor,fontSize: 14.sp),),
                          ),
                        ],
                      );
                    },
                  );
                },
              ),
              // GestureDetector(
              //   child:   Container(
              //     padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 8.h,),
              //     color: Colors.white,
              //     height: 40.h,
              //     child: Column(
              //       children: [
              //         Row(
              //           mainAxisAlignment: MainAxisAlignment.spaceBetween,
              //           children: [
              //             Text(messages["消息设置"],style: TextStyle(color: Colors.black,fontSize: 16.sp),),
              //             Row(
              //               children: [
              //                 Icon(Icons.arrow_forward_ios,color: MyColors.authTipColor,size: 14,),
              //               ],
              //             )
              //           ],
              //         ),
              //         SizedBox(height: 8.h,),
              //         Divider(height: 1,color: MyColors.backgreyColor,),
              //       ],
              //     ),
              //   ),
              //   onTap: () {
              //     Navigator.push( context, FadeRoute(page:MessageSetting()));
              //   },
              // ),
              // GestureDetector(
              //   child:   Container(
              //     padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 8.h,),
              //     color: Colors.white,
              //     height: 40.h,
              //     child: Column(
              //       children: [
              //         Row(
              //           mainAxisAlignment: MainAxisAlignment.spaceBetween,
              //           children: [
              //             Text(messages["语音设置"],style: TextStyle(color: Colors.black,fontSize: 16.sp),),
              //             Row(
              //               children: [
              //                 Icon(Icons.arrow_forward_ios,color: MyColors.authTipColor,size: 14,),
              //               ],
              //             )
              //           ],
              //         ),
              //         SizedBox(height: 8.h,),
              //         Divider(height: 1,color: MyColors.backgreyColor,),
              //       ],
              //     ),
              //   ),
              //   onTap: () {
              //     Navigator.push( context, FadeRoute(page:VoiceSetting()));
              //   },
              // ),
              GestureDetector(
                child:   Container(
                  padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 8.h,),
                  color: Colors.white,
                  height: 35.h,
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(messages["收货地址"],style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                          Row(
                            children: [
                              Icon(Icons.arrow_forward_ios,color: MyColors.authTipColor,size: 14,),
                            ],
                          )
                        ],
                      ),
                      SizedBox(height: 8.h,),
                      Divider(height: 1,color: MyColors.backgreyColor,),
                    ],
                  ),
                ),
                onTap: () {
                  Navigator.push( context, FadeRoute(page:UserAddress()));
                },
              ),

              GestureDetector(
                child:   Container(
                  padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 8.h,),
                  color: Colors.white,
                  height: 35.h,
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(messages["隐私"],style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                          Row(
                            children: [
                              Icon(Icons.arrow_forward_ios,color: MyColors.authTipColor,size: 14,),
                            ],
                          )
                        ],
                      ),
                      SizedBox(height: 8.h,),
                      Divider(height: 1,color: MyColors.backgreyColor,),
                    ],
                  ),
                ),
                onTap: () {
                  Navigator.push( context, FadeRoute(page:PrivacyPolicyList()));
                },
              ),

              GestureDetector(
                child:   Container(
                  padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 8.h,),
                  color: Colors.white,
                  height: 35.h,
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(messages["隐私政策管理"],style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                          Row(
                            children: [
                              Icon(Icons.arrow_forward_ios,color: MyColors.authTipColor,size: 14,),
                            ],
                          )
                        ],
                      ),
                      SizedBox(height: 8.h,),
                      Divider(height: 1,color: MyColors.backgreyColor,),
                    ],
                  ),
                ),
                onTap: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) {
                        return PrivacyPolicyManagement(
                        );
                      }));
                },
              ),
              // GestureDetector(
              //   child:   Container(
              //     padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 8.h,),
              //     color: Colors.white,
              //     height: 40.h,
              //     child: Column(
              //       children: [
              //         Row(
              //           mainAxisAlignment: MainAxisAlignment.spaceBetween,
              //           children: [
              //             Text(messages["告诉好友"],style: TextStyle(color: Colors.black,fontSize: 16.sp),),
              //             Row(
              //               children: [
              //                 Icon(Icons.arrow_forward_ios,color: MyColors.authTipColor,size: 14,),
              //               ],
              //             )
              //           ],
              //         ),
              //         SizedBox(height: 8.h,),
              //         Divider(height: 1,color: MyColors.backgreyColor,),
              //       ],
              //     ),
              //   ),
              //   onTap: () {
              //     showModalBottomSheet(context: context, builder: (BuildContext context)
              //     {
              //       return Container(
              //         decoration: BoxDecoration(
              //             color: Colors.white,
              //             borderRadius: BorderRadius.all(Radius.circular(10))),
              //
              //         height: 185.h,
              //         padding: EdgeInsets.only(top: 10.h,left: 15.w,right: 15.w),
              //         child: Column(
              //           children: [
              //             Row(
              //               mainAxisAlignment: MainAxisAlignment.spaceBetween,
              //               children: [
              //                 Text("分享到",style: TextStyle(color: Colors.black,fontWeight: FontWeight.w600),),
              //                 IconButton(onPressed: (){
              //                   Navigator.pop(context);
              //                 }, icon: Icon(Icons.close,color: MyColors.hintColor,size: 30,)),
              //               ],
              //             ),
              //
              //             Container(height: 1.h,color: MyColors.backgreyColor,),
              //             SizedBox(height: 10.h,),
              //             Row(
              //               mainAxisAlignment: MainAxisAlignment.spaceBetween,
              //               children: [
              //                 GestureDetector(
              //                   child: Column(
              //                       children:[
              //                         Image.asset("lib/Images/PersonCenter/分享到微信.png",width: 40.w,height: 40.h,),
              //                         Text("微信",style: TextStyle(color: Colors.black,fontSize: 12.sp),),
              //                       ]
              //                   ),
              //
              //                   onTap: (){
              //                     SSDKMap params = SSDKMap()
              //                       ..setGeneral(
              //                           "速配货司机版",
              //                           "分享",
              //                           [
              //                             "http://pic28.photophoto.cn/20130818/0020033143720852_b.jpg"
              //                             "http://www.sph56.cn"
              //                           ],
              //                           "",
              //                           "",
              //                           "http://www.sph56.cn",
              //                           "",
              //                           "",
              //                           "",
              //                           "",
              //                           SSDKContentTypes.miniProgram);
              //                     SharesdkPlugin.share(ShareSDKPlatforms.wechatSession, params,
              //                             (SSDKResponseState state, dynamic userdata, dynamic contentEntity,
              //                             SSDKError error) {
              //                           BrnToast.show(error.rawData, context);
              //
              //                         });
              //                   },
              //                 ),
              //                 GestureDetector(
              //                   child: Column(
              //                       children:[
              //                         Image.asset("lib/Images/PersonCenter/分享到朋友圈.png",width: 40.w,height: 40.h,),
              //                         Text("朋友圈",style: TextStyle(color: Colors.black,fontSize: 12.sp),),
              //                       ]
              //                   ),
              //
              //                   onTap: (){
              //                     SSDKMap params = SSDKMap()
              //                       ..setGeneral(
              //                           "速配货司机版",
              //                           "分享",
              //                           [
              //                             "http://pic28.photophoto.cn/20130818/0020033143720852_b.jpg"
              //                                 "http://www.sph56.cn"
              //                           ],
              //                           "",
              //                           "",
              //                           "http://www.sph56.cn",
              //                           "",
              //                           "",
              //                           "",
              //                           "",
              //                           SSDKContentTypes.miniProgram);
              //                     SharesdkPlugin.share(ShareSDKPlatforms.wechatTimeline, params,
              //                             (SSDKResponseState state, dynamic userdata, dynamic contentEntity,
              //                             SSDKError error) {
              //                           BrnToast.show(error.rawData, context);
              //
              //                         });
              //                   },
              //                 ),
              //                 GestureDetector(
              //                   child: Column(
              //                       children:[
              //                         Image.asset("lib/Images/PersonCenter/分享到QQ.png",width: 40.w,height: 40.h,),
              //                         Text("QQ",style: TextStyle(color: Colors.black,fontSize: 12.sp),),
              //                       ]
              //                   ),
              //
              //                   onTap: (){
              //                     SSDKMap params = SSDKMap()
              //                       ..setGeneral(
              //                           "速配货司机版",
              //                           "分享",
              //                           [
              //                             "http://pic28.photophoto.cn/20130818/0020033143720852_b.jpg"
              //                                 "http://www.sph56.cn"
              //                           ],
              //                           "",
              //                           "",
              //                           "http://www.sph56.cn",
              //                           "",
              //                           "",
              //                           "",
              //                           "",
              //                           SSDKContentTypes.miniProgram);
              //                     SharesdkPlugin.share(ShareSDKPlatforms.qq, params,
              //                             (SSDKResponseState state, dynamic userdata, dynamic contentEntity,
              //                             SSDKError error) {
              //                           BrnToast.show(error.rawData, context);
              //
              //                         });
              //                   },
              //                 ),
              //
              //                 GestureDetector(
              //                   child: Column(
              //                       children:[
              //                         Image.asset("lib/Images/PersonCenter/分享到复制链接.png",width: 40.w,height: 40.h,),
              //                         Text("复制链接",style: TextStyle(color: Colors.black,fontSize: 12.sp),),
              //                       ]
              //                   ),
              //
              //                   onTap: (){
              //                     _copyToClipboard('http://www.sph56.cn');
              //                   },
              //                 ),
              //               ],
              //             )
              //           ],
              //         ),
              //       );
              //     }
              //     );
              //   },
              // ),

              GestureDetector(
                child:   Container(
                  padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 8.h,),
                  color: Colors.white,
                  height: 35.h,
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(messages["关于我们"],style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                          Row(
                            children: [
                              Icon(Icons.arrow_forward_ios,color: MyColors.authTipColor,size: 14,),
                            ],
                          )
                        ],
                      ),
                      SizedBox(height: 8.h,),
                      Divider(height: 1,color: MyColors.backgreyColor,),
                    ],
                  ),
                ),
                onTap: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) {
                        return AboutSPH(
                        );
                      }));
                },
              ),
              GestureDetector(
                child:   Container(
                  padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 8.h,),
                  color: Colors.white,
                  height: 35.h,
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(messages["网络检测"],style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                          Row(
                            children: [
                              Icon(Icons.arrow_forward_ios,color: MyColors.authTipColor,size: 14,),
                            ],
                          )
                        ],
                      ),
                      SizedBox(height: 8.h,),
                      Divider(height: 1,color: MyColors.backgreyColor,),
                    ],
                  ),
                ),
                onTap: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) {
                        return NetworkDetection(
                        );
                      }));
                },
              ),
              GestureDetector(
                child:   Container(
                  padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 8.h,),
                  color: Colors.white,
                  height: 35.h,
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(messages["清除缓存"],style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                          Row(
                            children: [
                              Icon(Icons.arrow_forward_ios,color: MyColors.authTipColor,size: 14,),
                            ],
                          )
                        ],
                      ),
                      SizedBox(height: 8.h,),
                      Divider(height: 1,color: MyColors.backgreyColor,),
                    ],
                  ),
                ),
                onTap: () {
                  showDialog(
                    barrierDismissible: false,
                    context: context,
                    builder: (BuildContext context) {
                      return BrnDialog(
                        titleText:"是否清除应用缓存",
                        actionsWidget: [
                          TextButton(
                            onPressed: () {
                              Navigator.pop(context);
                            },
                            child: Text( messages['取消'],style: TextStyle(color: MyColors.textColor,fontSize: 14.sp),),
                          ),
                          TextButton(
                            onPressed: () async{
                              Directory directory = await getApplicationDocumentsDirectory();
                              //清楚缓存并重新获取缓存大小
                              CacheUtil.delDir(directory).then((value) => getAppCache());
                              setState(() {
                                appCache="0M";
                              });
                              BrnToast.show('缓存已清除', context);
                              Navigator.pop(context);
                            },
                            child: Text(messages['确定'],style: TextStyle(color: MyColors.themeColor,fontSize: 14.sp),),
                          ),
                        ],
                      );
                    },
                  );


                },
              ),
              GestureDetector(
                child:   Container(
                  padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 8.h,),
                  color: Colors.white,
                  height: 35.h,
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(messages["账户与安全"],style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                          Row(
                            children: [
                              Icon(Icons.arrow_forward_ios,color: MyColors.authTipColor,size: 14,),
                            ],
                          )
                        ],
                      ),
                      SizedBox(height: 8.h,),
                      Divider(height: 1,color: MyColors.backgreyColor,),
                    ],
                  ),
                ),
                onTap: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) {
                        return AccountAndSecurity(
                        );
                      }));
                },
              ),
            ],
          ),
          ),
          Container(
            height: 80.h,
            color: Colors.white,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                    height: 40.h ,
                    margin: EdgeInsets.only(left: 30.w,right: 30.w),
                    decoration: BoxDecoration(
                        color:Color.fromRGBO(231, 112, 27, 0.85),
                        borderRadius: BorderRadius.all(Radius.circular(30))),

                    width: double.infinity,


                    child: TextButton(
                      child: Text(messages["退出登录"],style: TextStyle(color: Colors.white,fontSize: 14.sp,fontWeight: FontWeight.w600),),

                      onPressed: (){
                        showDialog(
                          context: context,
                          builder: (BuildContext context) {
                            return BrnDialog(
                              titleText: '提示',
                              messageText:'确定退出当前账号？',
                              actionsWidget: [
                                TextButton(
                                  onPressed: () {
                                    Navigator.of(context).pop();
                                  },
                                  child: Text("取消",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                                ),
                                TextButton(
                                  onPressed: () {
                                    Navigator.of(context).pop();

                                    NoLoadinggetApiManager.getData(
                                      context, // 上下文对象，用于显示提示信息等
                                      'api/auth/auth/driver/doLogout', // 您的API端点
                                      {}, // 查询参数，如果没有可以传递null
                                          (responseData)  {
                                      },
                                    );
                                    TokenManager.clearToken();
                                    Navigator.of(context).pushAndRemoveUntil(
                                        MaterialPageRoute(builder: (builder) {
                                          return LoginVC(backType: 1,);
                                        }), (route) => route == null);
                                  },
                                  child: Text("确定",style: TextStyle(color: MyColors.themeColor,fontSize: 14.sp),),
                                ),
                              ],
                            );
                          },
                        );


                      },
                    )
                ),
              ],
            ),
          ),

        ],
      ),


    );
  }
}
